package com.hx.order.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.order.mapper.ItemMapper;
import com.hx.order.pojo.Item;
import com.hx.order.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Item> findAll() {
        return itemMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Item> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Item> items = (Page<Item>) itemMapper.selectAll();
        return new PageResult<Item>(items.getTotal(),items.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Item> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return itemMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Item> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Item> items = (Page<Item>) itemMapper.selectByExample(example);
        return new PageResult<Item>(items.getTotal(),items.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Item findById(Long id) {
        return itemMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param item
     */
    public void add(Item item) {
        itemMapper.insert(item);
    }

    /**
     * 修改
     * @param item
     */
    public void update(Item item) {
        itemMapper.updateByPrimaryKeySelective(item);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        itemMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 订单id
            if(searchMap.get("orderId")!=null && !"".equals(searchMap.get("orderId"))){
                criteria.andLike("orderId","%"+searchMap.get("orderId")+"%");
            }
            // 药品名
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            // 数量
            if(searchMap.get("num")!=null ){
                criteria.andEqualTo("num",searchMap.get("num"));
            }

        }
        return example;
    }

}
